//
//  HWRechargeNoteView.m
//  嗨文
//
//  Created by 响当当 on 2019/2/15.
//  Copyright © 2019年 dongzheng. All rights reserved.
//

#import "HWRechargeNoteView.h"
#import "HWRechargeNoteCell.h"
#import "HWTimeChooseView.h"

@interface HWRechargeNoteView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, copy)NSArray *rechargeNoteArr;//充值记录arr

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)HWTimeChooseView *timeView;//时间选择

@property (nonatomic, strong)NSString *timeStr;//当前年月
@property (nonatomic, strong)NSString *type;//区别充值记录 消费记录 vip充值记录
@end

@implementation HWRechargeNoteView

- (NSArray *)rechargeNoteArr
{
    if (!_rechargeNoteArr)
    {
//        _rechargeNoteArr = @[@{@"logo":@"支付宝",@"title":@"购买嗨读VIP3个月",@"time":@"01月02日 10:58",@"money":@"XXX"},
//                             @{@"logo":@"微信",@"title":@"购买嗨读VIP12个月",@"time":@"01月02日 10:58",@"money":@"XXX"},
//                             @{@"logo":@"支付宝",@"title":@"购买嗨读VIP1个月",@"time":@"01月02日 10:58",@"money":@"XXX"},
//                             @{@"logo":@"微信",@"title":@"购买嗨读VIP6个月",@"time":@"01月02日 10:58",@"money":@"XXX"},];
        _rechargeNoteArr = [[NSArray alloc]init];
    }
    return _rechargeNoteArr;
}

- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = 0;
        _tableView.backgroundColor = LCHexColor(0xF5F5F5);
    }
    return _tableView;
}

- (HWTimeChooseView *)timeView
{
    if (!_timeView)
    {
        _timeView = [[HWTimeChooseView alloc]initWithFrame:self.bounds];
        WeakSelf(self)
        _timeView.getDateBlock = ^(NSString *timeStr) {
            weakSelf.timeStr = timeStr;
            NSArray *yearArr = [timeStr componentsSeparatedByString:@"年"];
            [weakSelf getYearAndMonth:yearArr];
        };
    }
    return _timeView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self addSubview:self.tableView];
    }
    return self;
}

#pragma mark - delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.rechargeNoteArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 68*kUIScaleBasePlus;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 38*kUIScaleBasePlus;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,38*kUIScaleBasePlus)];
    backView.backgroundColor = LCHexColor(0xF5F5F5);
    
    UILabel *timeLabel = [HWUtilties creatLabelWithFrame:CGRectMake(17*kUIScaleBasePlus,0,70*kUIScaleBasePlus,38*kUIScaleBasePlus)
                                            textAligment:NSTextAlignmentLeft
                                                textClor:LCHexColor(0x6E6E6E)
                                                textFont:12*kUIScaleBasePlus];
    if (!self.timeStr)
    {
        timeLabel.text = [HWUtilties getYearAndMonth];
    }
    else
    {
        timeLabel.text = self.timeStr;
    }
    
    [backView addSubview:timeLabel];
    
    UIButton *chooseTimeBtn = [HWUtilties creatButtonWithFrame:CGRectMake(CGRectGetMaxX(timeLabel.frame)+3*kUIScaleBasePlus,13.5*kUIScaleBasePlus,11*kUIScaleBasePlus,11*kUIScaleBasePlus)
                                                  textAligment:NSTextAlignmentLeft
                                                          text:@""
                                                      textClor:nil
                                                     buttonBGC:[UIColor clearColor]
                                                      textFont:0
                                                        target:self
                                                        method:@selector(chooseTime)];
    [chooseTimeBtn setImage:[UIImage imageNamed:@"time_selected"] forState:UIControlStateNormal];
    [backView addSubview:chooseTimeBtn];

    return backView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HWRechargeNoteCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HWRechargeNoteCell"];
    if (!cell)
    {
        cell = [[HWRechargeNoteCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HWRechargeNoteCell"];
        cell.selectionStyle = 0;
    }
    
    if ([self.type isEqualToString:@"0"])
    {
        HWRechargeNoteModel *model = [[HWRechargeNoteModel alloc]initWithDic:self.rechargeNoteArr[indexPath.row]];
        [cell getRechargeNoteDate:model];
    }
    else
    {
        HWConsumeModel *model = [[HWConsumeModel alloc]initWithDic:self.rechargeNoteArr[indexPath.row]];
        [cell getConumeDate:model];
    }
    
    return cell;
}

#pragma mark - 选择时间
- (void)chooseTime
{
    [self addSubview:self.timeView];
}

- (void)getRechargeNoteDate:(NSArray *)arr Type:(NSString *)type
{
    self.rechargeNoteArr = [NSArray arrayWithArray:arr];
    self.type = type;
    [self.tableView reloadData];
}

- (void)getYearAndMonth:(NSArray *)yearArr
{
    NSString *month = yearArr[1];
    NSArray *monthArr = [month componentsSeparatedByString:@"月"];
    if (self.getYearAndMonthBlock)
    {
        self.getYearAndMonthBlock(yearArr[0],monthArr[0]);
    }
}
@end
